package com.easy.service.impl;

import com.easy.bean.User_activity;
import com.easy.mapper.User_activityDao;
import com.easy.service.User_activityService;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class User_activityServiceImpl implements User_activityService {

    private static final String CACHE_NAME = "user_activity";

    @Autowired
    User_activityDao user_activityDao;

    @Override
    @CacheEvict(value = CACHE_NAME, key = "'user_activity:' + #p0.id", condition = "#result != null")
    public User_activity add(User_activity user_activity) {
        user_activityDao.add(user_activity);
        return user_activityDao.one(user_activity.getId());
    }

    @Override
    @CacheEvict(value = CACHE_NAME, key = "'user_activity:id:' + #p0")
    public int del(Integer id) {
        return user_activityDao.del(id);
    }

    @Override
    @CacheEvict(value = CACHE_NAME, key = "'user_activity:' + #p0.id", condition = "#p0 != null and #p0.id != null")
    public User_activity edit(User_activity user_activity) {
        user_activityDao.edit(user_activity);
        return user_activityDao.one(user_activity.getId());
    }

    @Override
    @Cacheable(value = CACHE_NAME, key = "'user_activity:list:' + T(java.util.Objects).hash(#p0)", unless = "#result == null or #result.isEmpty()")
    public List<User_activity> all(User_activity user_activity) {
        return user_activityDao.all(user_activity);
    }

    @Override
    @Cacheable(value = CACHE_NAME, key = "'user_activity:id:' + #p0", unless = "#result == null")
    public User_activity one(Integer id) {
        return user_activityDao.one(id);
    }

    @Override
    public void page(Page page, User_activity user_activity) {
        int count = user_activityDao.pageCount(user_activity);
        Object data = user_activityDao.page(page, user_activity);
        page.setCount(count);
        page.setData(data);
    }

    @Override
    public void pageWithDetails(Page page, User_activity user_activity) {
        int count = user_activityDao.pageCountWithDetails(user_activity);
        Object data = user_activityDao.pageWithDetails(page, user_activity);
        page.setCount(count);
        page.setData(data);
    }
}
